package com.sunjq.fetchdata.internet;



import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetDataFromInternetUtils {

	private String value = "";

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public static String getDataFromUrl(String url) {

		HttpClient httpclient = new DefaultHttpClient();
		String result = "";
		try {

			HttpGet httpget = new HttpGet(url);
			// System.out.println("executing request " + httpget.getURI());

			// Create a response handler
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			result = httpclient.execute(httpget, responseHandler);
	
//			if(result!=null && !"".equals(result)) {      
//	            Pattern p = Pattern.compile("\r|\n");      
//	            Matcher m = p.matcher(result);      
//	            String strNoBlank = m.replaceAll("");      
//	            return strNoBlank;      
//	        }else {      
//	            return result;      
//	        }  

			// this.value = value;

			return result;

		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();

		} finally {
			// When HttpClient instance is no longer needed,
			// shut down the connection manager to ensure
			// immediate deallocation of all system resources
			httpclient.getConnectionManager().shutdown();
		}
	}
}
